<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>金额转换</title>
	</head>
	<body>
		<table border="0" cellspacing="0" cellpadding="2">
			<tr>
				<td>输入数字：</td>
				<td><input type="text" id="in" /></td>
				<td><button type="button" onclick="fun1()">转换</button></td>
			</tr>
			<tr>
				<td>银行金额：</td>
				<td><span id="sp1"></span></td>
			</tr>
			<tr>
				<td>大写金额：</td>
				<td><span id="sp2"></span></td>
			</tr>
		</table>
		<script>
			var fun1 = function() {
				var a = [];
				var i = 0;
				var k = 0;
				var num = document.getElementById("in").value;
				if (isNaN(num)) {
					alert("请输入正确的数字！！");
					return;
				} else if (num == '') {
					alert("输入不能为空！！！！");
					return;
				} else {
					while (true) {
						if (num[i] != "0") {
							for (var m = i; m < num.length; m++) {
								a[k++] = num[m];
							}
							break;
						} else {
							i++;
						}
					}
					if (a.length == 0) {
						document.getElementById("sp1").innerHTML = Number(0).toFixed(2);
						document.getElementById("sp2").innerHTML = "零元零角零分";
					} else {
						var str = ''
						for (var j = 0; j < a.length; j++) {
							str = str + a[j];
						}
						var big = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
						var oo = [
							"#",
							"元",
							"拾",
							"佰",
							"仟",
							"万",
							"拾",
							"百",
							"千",
							"亿",
							"拾",
							"百",
							"千",
							"万"
						];
						var d = ["元", "角", "分"];
						var result = [];
						//完全是一个小数的情况
						if (str.indexOf(".") == 0) {
							var mon = Number(str).toFixed(2);
							var k = 0;
							document.getElementById("sp1").innerHTML = mon;
							var ss = "零元" + big[mon[2]].toString() + "角" + big[mon[3]].toString() + "分";
							document.getElementById("sp2").innerHTML = ss;


						} else if (str.indexOf(".") == -1) {
							var low = str.length;
							var len = 3;
							var index = 0;
							while (low > 0) {
								if (low - 3 <= 0) {
									result[index++] = str.substring(0, low);
									low = low - len;
								} else {
									result[index++] = str.substring(low - 3, low);
									low = low - len;
								}
							}
							var money = '';
							for (var i = 0; i < result.length; i++) {
								money = result[i] + "," + money;
							}
							money = money.substring(0, money.length - 1) + ".00";
							document.getElementById("sp1").innerHTML = money;


							//处理中文
							var aim = '';
							var q = a.length;
							var temp = ["#"];
							var k = 1;
							for (var i = 0; i < a.length; i++) {
								temp[k++] = a[i];
							}
							for (var i = 1; i <= temp.length - 1; i++) {
								aim = aim + big[temp[i]] + oo[q--];
							}
							document.getElementById("sp2").innerHTML = aim;
						} else if (str.indexOf(".") != 0 && str.indexOf(".") != -1) {
							var mm = [];
							mm = str.split(".");
							//处理小写金额
							var low = mm[0].length;
							var len = 3;
							var index = 0;
							var result1=[];
							while (low > 0) {
								if (low - 3 <= 0) {
									result1[index++] = mm[0].substring(0, low);
									low = low - len;
								} else {
									result1[index++] = mm[0].substring(low - 3, low);
									low = low - len;
								}
							}
							var money = '';
							for (var i = 0; i < result1.length; i++) {
								money = result1[i] + "," + money;
							}
							var tt="0."+mm[1];
							tt=Number(tt).toFixed(2);
							money = money.substring(0,money.length-1)+tt[1]+tt[2]+tt[3];
							document.getElementById("sp1").innerHTML=money;
							
							//处理大写金额
							var temp1 = [];
							for (var i = 0; i < mm[0].length; i++) {
								temp1[i] = mm[0][i];
							}
							var aim = '';
							var q = temp1.length;
							var temp = ["#"];
							var k = 1;
							for (var i = 0; i < temp1.length; i++) {
								temp[k++] = temp1[i];
							}
							for (var i = 1; i <= temp.length - 1; i++) {
								aim = aim + big[temp[i]] + oo[q--];
							}
							var ss = big[tt[2]].toString() + "角" + big[tt[3]].toString() + "分";
							document.getElementById("sp2").innerHTML=aim+ss;
						}
					}

				}
			}
		</script>
	</body>
</html>
